/**
 * Simple lexer application, parse input tokens.
 * Add two numbers from the parse script.
 * This implementation done, Berlin Brown (berlin dot brown at gmail.com)
 * Variation on code from : "Language Implementation Patterns", Terence Parr
 */
package org.berlin.octane.compiler.lang;

/**
 * Visitor implementation.  Visit each node and
 * handle accordingly.
 */
public class SimpleAdderVisitor implements IAdderVisitor {

    private int lastResult = 0;
    
    public void visit(final PlusNode n) {        
        System.out.print("-Visit:Plus:{" + n.toString() + "}- ");        
        n.getArgOne().visit(this);
        final int arg1 = this.lastResult;
        n.getArgTwo().visit(this);
        final int arg2 = this.lastResult;
        this.lastResult = arg1 + arg2;
    }

    public void visit(final IntegerNode n) {
        this.lastResult = Integer.parseInt(n.getToken().getText());
        System.out.println(" -Visit:Integer:{" + n.toString() + "}- ");        
    }

    /**
     * @return the lastResult
     */
    public int getLastResult() {
        return lastResult;
    }

} // End of the class //
